#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QThread>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tcpServer = new TcpServer;
    tcpClient = new TcpClient;

    QThread *t1 = new QThread;
    QThread *t2 = new QThread;


    connect(this      ,&MainWindow::startListen,        tcpServer       ,&TcpServer::startListen                ,Qt::QueuedConnection);
    connect(this      ,&MainWindow::serverSendData,     tcpServer       ,&TcpServer::sendData                   ,Qt::QueuedConnection);
    connect(this      ,&MainWindow::clientConnectTo,    tcpClient       ,&TcpClient::clientConnectTo            ,Qt::QueuedConnection);
    connect(this      ,&MainWindow::clientCloseConnect, tcpClient       ,&TcpClient::closeConnect               ,Qt::QueuedConnection);
    connect(this      ,&MainWindow::clientSendData,     tcpClient       ,&TcpClient::sendData                   ,Qt::QueuedConnection);
    connect(this      ,&MainWindow::serverInit,         tcpServer       ,&TcpServer::init                       ,Qt::QueuedConnection);
    connect(this      ,&MainWindow::clientInit,         tcpClient       ,&TcpClient::init                       ,Qt::QueuedConnection);
    connect(this      ,&MainWindow::serverSendFile,     tcpServer       ,&TcpServer::sendFile                   ,Qt::QueuedConnection);
    connect(this      ,&MainWindow::clientSendFile,     tcpClient       ,&TcpClient::sendFile                   ,Qt::QueuedConnection);
    connect(tcpServer ,&TcpServer::signReadyRead,       this            ,&MainWindow::slotServerReadyread       ,Qt::QueuedConnection);
    connect(tcpClient ,&TcpClient::signReadyRead,       this            ,&MainWindow::slotClientReadyread       ,Qt::QueuedConnection);
    connect(tcpClient ,&TcpClient::signConnected,       this            ,&MainWindow::slotClientConnected       ,Qt::QueuedConnection);
    connect(tcpClient ,&TcpClient::signDisconnected,    this            ,&MainWindow::slotClientDisconnected    ,Qt::QueuedConnection);
    connect(tcpClient ,&TcpClient::signDisplayError,    this            ,&MainWindow::slotClientDisplayError    ,Qt::QueuedConnection);
    tcpServer->moveToThread(t1);
    tcpClient->moveToThread(t2);
    t1->start();
    t2->start();

    serverInit();
    clientInit();

    //接受拖放文件
    this->setAcceptDrops(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_start_listen_btn_clicked()
{
    QString ip = ui->listen_ip_line->text();
    QString port = ui->listen_port_line->text();
    startListen(ip,port);
    //tcpServer->startListen(ip,port);
}

void MainWindow::on_server_send_btn_clicked()
{
    QString senddata = ui->server_sendText_line->text();
    serverSendData(senddata);
    //tcpServer->sendData(senddata);
}

void MainWindow::on_client_connect_btn_clicked()
{
    if(ui->client_connect_btn->text() == "连接"){
        QString ip = ui->dst_ip_line->text();
        QString port = ui->dst_port_line->text();
        clientConnectTo(ip,port);
        //tcpClient->clientConnectTo(ip,port);
    }else {
        clientCloseConnect();
        //tcpClient->closeConnect();
    }
}

void MainWindow::on_client_send_btn_clicked()
{
    QString data = ui->client_sendText_line->text();
    clientSendData(data);
    //tcpClient->sendData(data);
}

void MainWindow::slotServerReadyread(TcpProtocol p)
{
    ui->server_recvText_line->append(QString(p.data));
}

void MainWindow::slotClientReadyread(TcpProtocol p)
{
    ui->client_recvText_line->append(QString(p.data));
}

void MainWindow::slotClientConnected()
{
    ui->client_connect_btn->setText("断开");
}

void MainWindow::slotClientDisconnected()
{
    ui->client_connect_btn->setText("连接");
}

void MainWindow::slotClientDisplayError(QAbstractSocket::SocketError  err)
{
    ui->client_recvText_line->append(QString(err));
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    event->acceptProposedAction();
}

void MainWindow::dropEvent(QDropEvent *event)
{
    QString name = event->mimeData()->urls().first().toString().split("/").last();
    QString path = event->mimeData()->urls().first().path().replace(0,1,"");
    ui->lineEdit->setText(name);
    ui->lineEdit_2->setText(path);
}

void MainWindow::on_server_file_send_btn_clicked()
{
    serverSendFile(ui->lineEdit_2->text());
}

void MainWindow::on_client_file_send_btn_clicked()
{
    clientSendFile(ui->lineEdit_2->text());
}
